<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5">
{
  style: {
    navigationBarTitleText: '数据下载',
    navigationBarBackgroundColor: '#1989fa',
    navigationBarTextStyle: 'white',
  },
}
</route>
<template>
  <Loyout class="loaddata-page" bgc="#F3F8FE">
    <view class="loaddata-box">
      <!-- <view class="p-30">
        <wd-input class="p-10" type="text" v-model="fileName" placeholder="请输入下载文件名称" />
        <wd-button class="flex1 ml-20" size="large" @click="loadData">更新信息</wd-button>
      </view>
      <wd-picker :columns="columns" label="数据类型" v-model="type" /> -->
    </view>
    <template #footer>
      <wd-button class="flex1 ml-20" size="large" @click="loadData">更新机构</wd-button>
    </template>
  </Loyout>
</template>

<script lang="ts" setup>
import Loyout from '@/layouts/index.vue'
import { useToast } from 'wot-design-uni'
import { getTextUrlStr, getXlsxUrlStr } from '@/utils'
import { useUserStore } from '@/store/user'
import { useSocket } from '@/utils/socket'

const formData = reactive({
  account: 'admin',
  password: '1',
})

const userStore = useUserStore()
const Toast = useToast()
const columns = [{ label: '往来单位信息', value: '1' }]
const fileName = ref('')
const type = ref('')

const loadData = async () => {
  // if (!fileName.value) return Toast.error('请输入下载文件名称')
  await useSocket({
    type: 'updateCustomer',
    callback: (data) => {
      if (data?.length) {
        const list = data.map((item) => {
          const itemArrs = item.split(',')
          return {
            label: itemArrs[0],
            value: itemArrs[2],
            abbreviation: itemArrs[1],
          }
        })
        userStore.setOrgs(list)
        Toast.success('更新成功')
      }
    },
  })

  uni.sendSocketMessage({
    data: 'updateCustomer',
    complete: (complete) => {
      console.log('complete', complete)
    },
    fail: (fail) => {
      console.log('fail', fail)
    },
  })
}

const getFileUrl = (fileName: string) => {
  if (fileName.includes('http')) return fileName
  return `http://${userStore.userInfo.ip}:${userStore.userInfo.port}/${fileName}`
}
</script>
